<?php

namespace app\api\controller;
use app\BaseController;
use app\service\log\UserOperateLogService;
use app\service\system\SystemSettingService;
use app\util\CommonUtil;
use app\util\ResponseUtil;
use app\util\UserUtil;

class SystemSetting extends BaseController
{
    public function getSystemSettingInfo()
    {
        $id = $this->requestData['id'];
        $service = new SystemSettingService();
        $finalData = $service->getSystemSettingInfoById($id);
        return ResponseUtil::success($finalData);
    }

    public function getSystemSettingList()
    {
        $pageIndex = $this->requestData['page'];
        $pageSize = $this->requestData['rows'];
        $service = new SystemSettingService();
        $list = $service->getSystemSettingList($this->requestData, $pageIndex, $pageSize);
        $total = $service->getSystemSettingTotalNum($this->requestData);
        $finalData = CommonUtil::getPaginationData($list, $total);
        return ResponseUtil::success($finalData);
    }

    public function getSystemSettingOption()
    {
        $service = new SystemSettingService();
        $list = $service->getSystemSettingOption($this->requestData);
        return ResponseUtil::success($list);
    }

    public function createSystemSetting()
    {
        try{
            if(empty($this->requestData['title_cn'])){
                return ResponseUtil::failure("请填写设置项");
            }
            if(empty($this->requestData['content'])){
                return ResponseUtil::failure("请填写设置项内容");
            }
            $service = new SystemSettingService();
            $res = $service->addSystemSetting($this->requestData);
            if($res){
                $addInfo = CommonUtil::getDataInsertMessage($res, ['title_cn' => '设置项', 'content'=>'设置项内容']);
                $messageCn = '新增任务项,'.$addInfo;
                $messageEn = '';
                $logData = [
                    'subject_id' => $res['new_id'],
                    'remark'  => $res['task_item_no'],
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,

                ];
                UserUtil::addOperateLog('SystemSetting', UserOperateLogService::ARR_ACTION_TYPE['add'], $logData);
                return ResponseUtil::success($res);
            }else{
                return ResponseUtil::failure("新增失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('SystemSetting', 'addSystemSetting',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("新增失败");
        }
    }

    public function modifySystemSetting()
    {
        try{
            if(empty($this->requestData['id'])){
                return ResponseUtil::failure("请指定要编辑的任务");
            }
            if(empty($this->requestData['title_cn'])){
                return ResponseUtil::failure("请填写设置项");
            }
            if(empty($this->requestData['content'])){
                return ResponseUtil::failure("请填写设置项内容");
            }
            $id = $this->requestData['id'];
            $service = new SystemSettingService();
            $oldInfo = $service->getSystemSettingInfoById($id);
            if(empty($oldInfo)){
                return ResponseUtil::failure("任务不存在");
            }
            $res = $service->editSystemSettingById($id, $this->requestData);
            if($res){
                $changeInfo = CommonUtil::getChangeData($oldInfo, $this->requestData,['title_cn' => '设置项', 'content'=>'设置项内容', 'status'=>'状态']);
                $messageCn = '编辑任务项,'.$changeInfo['update_info'];
                $messageEn = '';
                $logData = [
                    'subject_id' => $id,
                    'remark'  => $oldInfo['title_cn'],
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('SystemSetting', UserOperateLogService::ARR_ACTION_TYPE['edit'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("编辑失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('SystemSetting', 'editSystemSetting',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("编辑失败");
        }
    }

    public function trashSystemSetting()
    {
        try{
            $id = $this->requestData['id'];
            $service = new SystemSettingService();
            $info = $service->getSystemSettingInfoById($id);
            if(!$info){
                return ResponseUtil::failure("数据不存在");
            }
            $res = $service->deleteSystemSettingById($id);
            if($res){
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('SystemSetting', 'deleteSystemSetting', CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除失败");
        }
    }

    public function deleteMultiSystemSetting(){
        try{
            $id = $this->requestData['id'];
            if(!is_array($id)){
                return ResponseUtil::failure("参数格式有误");
            }
            $service = new SystemSettingService();
            $deleteData = [];
            foreach($id as $value){
                $info = $service->getSystemSettingInfoById($value);
                if(!$info){
                    return ResponseUtil::failure("数据不存在");
                }
                $deleteData[] = $info;
            }
            $res = $service->deleteSystemSettingByManyId($id);
            if($res){
                $deleteInfo = CommonUtil::getDataDeleteMessage($deleteData, ['title_cn', 'content']);
                $messageCn = '删除任务项,'.$deleteInfo;
                $messageEn = '';
                $logData = [
                    'subject_id' => json_encode($id),
                    'remark'  => implode(',', array_column($deleteData, 'title_cn')),
                    'description_en' => $messageEn,
                    'description_cn' => $messageCn,
                ];
                UserUtil::addOperateLog('SystemSetting', UserOperateLogService::ARR_ACTION_TYPE['delete'], $logData);
                return ResponseUtil::success();
            }else{
                return ResponseUtil::failure("删除失败");
            }
        }catch(\Exception $e){
            CommonUtil::addErrorLog('SystemSetting', 'deleteMultiSystemSetting',  CommonUtil::getExceptionMessage($e));
            return ResponseUtil::failure("删除失败");
        }
    }


}



